package com.jy.servlet;

import com.jy.dao.UserDao;
import com.jy.dao.impl.UserDaoImpl;
import com.jy.entity.User;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) {
		String name = request.getParameter("act");
		String pass = request.getParameter("pwd");
		System.out.println(name);
		System.out.println(pass);
		try {
			UserDao user = new UserDaoImpl();
			User u = user.getUser(name, pass);
			System.out.println(u);
			if(u==null){
				System.out.println("空的");
				request.getSession().setAttribute("msg","用户名或密码错误");
				response.sendRedirect("/quanxian/login.jsp");
			}else{
				List auths=user.getauthority(u.getUid());//权限列表
				request.getSession().setAttribute("auths",auths);
				request.getSession().setAttribute("LOGIN",u);
				request.getSession().setAttribute("id",u.getUid());
				response.sendRedirect("/quanxian/index.jsp");
			}
		} catch (Exception throwables) {
			throwables.printStackTrace();
		}
	}
}